
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-03-16
 * Time: 16:01
 */
public class JDBCDEmo2 {
    public static void main(String[] args) throws SQLException {
        /**
         * 对数据库进行手动增加操作
         */
        Scanner scanner=new Scanner(System.in);
        //1.创建数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl
                ("jdbc:mysql://127.0.0.1:3306/java111?characterEncoding=utf8&useSSL=false");

        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("11111111");
        //2建立连接
       Connection connection= dataSource.getConnection();
        //3构造一个操作数据库的sql
        String sql="insert into student values(?,?)";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        System.out.println("请输入学号");
        int id=scanner.nextInt();
        System.out.println("请输入姓名;");
        String name=scanner.next();
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);
        //4,执行sql
        int n=preparedStatement.executeUpdate();
        System.out.println(n);
        //5.执行完毕,释放资源
        preparedStatement.close();
        connection.close();

    }
}
